﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using System.Text;
using System.Collections.Generic;
using DotNet.Common;
public partial class Admin_addProductAttribute : System.Web.UI.Page
{
    ProductAttributeBll proAttrBll = new ProductAttributeBll();
    AttributeBll atrBll = new AttributeBll();
    productBll pBll = new productBll();
    private int selectIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {
                string action = Request.QueryString["action"].ToString();
                switch (action.Trim())
                {
                    case "edit":
                        edti();
                        break;
                    case "del":
                        del();
                        break;
                    case "save":
                        Save();
                        break;
                    default:
                        break;
                }
            }
            bind();
        }
    }
    #region 绑定属性
    public void bind() 
    {
        int pid = Convert.ToInt32(Request.QueryString["pid"]);
        int atrID = 0;
        if (Request.QueryString["attributeID"]!=null)
        {
            atrID = Convert.ToInt32(Request.QueryString["attributeID"]);
        }
        this.Repeater1.DataSource = proAttrBll.pagingForProductAttribute(1, 10, atrID, pid);
        this.Repeater1.DataBind();
        product product = pBll.GetModel(pid);
        this.productName_txt.Value =product.productName;        
        this.Hidden1.Value = product.productId.ToString();
        selectIndex = atrID;
        Session["FCKeditor:UserFilesPath"] = "~/UploadFiles/productUpload/" + product.Productmodel;
    }
    #endregion   

    #region 编辑属性
    public void edti() 
    {
        int proAttrID = Convert.ToInt32(Request.QueryString["productattributeid"]);
        ProductAttribute proAttr = proAttrBll.GetModel(proAttrID);
        this.content.Value = proAttr.productAttributeUrl;
        this.Hidden2.Value = proAttrID.ToString();
        this.productAttributeVlaue_txt.Value = proAttr.productAttributeValue;
    }
    #endregion  

    #region 删除 
    public void del() 
    {
        int proAttrID = Convert.ToInt32(Request.QueryString["productattributeid"]);
        ProductAttribute proAttr = proAttrBll.GetModel(proAttrID);
        if (proAttrBll.Delete(proAttrID))
        {
            
            DirFile.DeleteFile(proAttr.productAttributeUrl.Substring(1,proAttr.productAttributeUrl.Length-1));
        }
    }
    #endregion

    #region 保存
    public void Save() 
    {
        if (Request.QueryString["productattributeid"] != null && Request.QueryString["productattributeid"]!="")
        {
            int productAttributeID = Convert.ToInt32(Request.QueryString["productattributeid"]);
            ProductAttribute proAttr = proAttrBll.GetModel(productAttributeID);
            string url = proAttr.productAttributeUrl.Substring(1,proAttr.productAttributeUrl.Length-1);     
            proAttr.productAttributeUrl = Request.Form["url"].ToString();
            proAttr.productAttributeID = productAttributeID;
            proAttr.productAttributeValue = Request.QueryString["attrValue"].ToString();
            if (proAttr.productAttributeValue.Trim()=="")
            {
                Response.Write("2");
                Response.End();
            }
            if (proAttrBll.Update(proAttr))
            {
                DirFile.DeleteFile(url); 
            }
        }
        else
        {
            Response.Write("1");
            Response.End();
        }
    }
    #endregion

    #region 根据属性类别id获得属性名称
    public string getAttributeName(object attributeID)
    {
        int attrID = Convert.ToInt32(attributeID);
        Attribute attr = atrBll.GetModel(attrID);
        return attr.attributeName;
    } 
    #endregion

    #region 下拉框
    public string attributeDp(string name)
    {
        int pid = Convert.ToInt32(Request.QueryString["pid"]);
        StringBuilder str = new StringBuilder();
        str.Append("<select onchange='change()' name='selectDp' id='" + name + "' style='width:192px;'>");
        str.Append("<option value='0'>---未选择属性---</option>");
        IList<Attribute> attriList = atrBll.GetModel();
        foreach (Attribute atr in attriList)
        {
            if (proAttrBll.Exists(atr.attributeID,pid))
            {
                if (selectIndex == atr.attributeID)
                {
                    str.AppendFormat("<option selected='selected' value='" + atr.attributeID + "'>{0}</option>", atr.attributeName);
                }
                else
                    str.AppendFormat("<option value='" + atr.attributeID + "'>{0}</option>", atr.attributeName); 
            }          
        }       
        str.Append("</select>");
        return str.ToString();
    }   
    #endregion


}
